MODULE menu_read
  !-----------------------------------------------------------
  ! KRUUNUNHAKA basis set tool kit - basis set read-in menu
  !-----------------------------------------------------------
  ! See CHANGELOG
  !-----------------------------------------------------------
  ! Copyright (C) 2003-2008 Pekka Manninen, 2010 Jussi Lehtola
  !
  ! This program is distributed under the terms of the GNU General
  ! Public License as published by the Free Software Foundation;
  ! either version 2 of the License, or (at your option) any later version.
  !
  ! This program is distributed in the hope that it will be useful,
  ! but WITHOUT ANY WARRANTY; without even the implied warranty of
  ! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
  !
  ! Please note that this program as well as its 
  ! author must be properly cited whenever the program or some parts 
  ! originated on it are employed. 
  !
  !-----------------------------------------------------------

  USE definitions
  USE textutil
  USE inout
  USE menu

  IMPLICIT NONE

CONTAINS

  SUBROUTINE read_menu(X_l,C_l,nbas)
    IMPLICIT NONE
    REAL(KIND=prec), DIMENSION(:,:), ALLOCATABLE, INTENT(INOUT) :: X_l
    REAL(KIND=prec), DIMENSION(:,:,:), ALLOCATABLE, INTENT(INOUT) :: C_l
    INTEGER, DIMENSION(MAXL,2), INTENT(INOUT) :: nbas

    CHARACTER(LEN=TERMWIDTH) :: readmenu_tit
    CHARACTER(LEN=TERMWIDTH), DIMENSION(4) :: readmenu
    CHARACTER(LEN=4) :: readsel

    CHARACTER(LEN=TERMWIDTH), SAVE :: filename=' '

    INTEGER :: sel

    LOGICAL :: deallocok, readok, fileex

    readmenu_tit="Read menu"
    readmenu(1)='Set file name'
    readmenu(2)='Read basis set from file in Dalton format'
    readmenu(3)='Read basis set from file in Gaussian''94 format'
    readmenu(4)='Return to main menu'
    readsel='123r'

    ! OK to deallocate?
    deallocok=.TRUE.

    ! We have a basis set in memory
    IF(ALLOCATED(X_l) .OR. ALLOCATED(C_l)) THEN
       deallocok=make_yn("Loading basis set from file will erase changes!")
       WRITE(*,*) ''
       IF(deallocok) THEN
          ! OK to erase memory.
          IF(ALLOCATED(X_l)) DEALLOCATE(X_l)
          IF(ALLOCATED(C_l)) DEALLOCATE(C_l)
       ELSE
          RETURN
       END IF
    END IF

    ! If OK, go to read menu.
    readok=.FALSE.
    DO WHILE(.NOT. readok)     
       IF(LEN(TRIM(filename))>0) THEN
          write(readmenu_tit,'(A,A)') "Read menu, reading input from ",TRIM(filename)
       ELSE
          readmenu_tit="Read menu"
       END IF
       
       ! Print out menu
       sel=make_selection(readmenu_tit,readmenu,readsel,'*')
       
       SELECT CASE(sel)
       CASE(1)
          ! Set input file name
          WRITE (*,'(A)',advance='no') 'Give input file name: '
          READ (*,*) filename
          WRITE (*,*) ''
       CASE(2)
          ! Check if file exists
          INQUIRE(FILE=filename, EXIST=fileex)

          IF(fileex) THEN
             ! Get number of basis functions
             CALL read_dalton_nbas(filename,nbas)
             ! Allocate memory
             ALLOCATE(X_l(MAXL,MAXVAL(nbas(:,1))))
             ALLOCATE(C_l(MAXL,MAXVAL(nbas(:,1)),MAXVAL(nbas(:,1))))
             ! Read in basis
             CALL read_dalton_coeffs(filename,nbas,X_l,C_l)
             readok=.TRUE.
          ELSE
             WRITE (*,'(A,A,A)') 'The file ', TRIM(FILENAME), ' does not exist!'
          END IF
       CASE(3)
          ! Check if file exists
          INQUIRE(FILE=filename, EXIST=fileex)

          IF(fileex) THEN
             ! Get number of basis functions
             CALL read_gaussian_nbas(filename,nbas)
             ! Allocate memory
             ALLOCATE(X_l(MAXL,MAXVAL(nbas(:,1))))
             ALLOCATE(C_l(MAXL,MAXVAL(nbas(:,1)),MAXVAL(nbas(:,1))))
             ! Read in basis
             CALL read_gaussian_coeffs(filename,X_l,C_l)
             readok=.TRUE.
          ELSE
             WRITE (*,'(A,A,A)') 'The file ', TRIM(FILENAME), ' does not exist!'
          END IF
       CASE(4)
          readok=.TRUE.
       END SELECT
    END DO
    WRITE (*,*) ''

  END SUBROUTINE read_menu
END MODULE menu_read
